<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Path Hierarchy Tokenizer Examples | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'analysis-pathhierarchy-tokenizer-examples.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/analysis-pathhierarchy-tokenizer-examples.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/analysis-pathhierarchy-tokenizer-examples.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/analysis-pathhierarchy-tokenizer-examples.html" rel="nofollow" target="_blank">../en/analysis-pathhierarchy-tokenizer-examples.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="analysis.html">Text analysis</a></span>
»
<span class="breadcrumb-link"><a href="analysis-tokenizers.html">Tokenizer reference</a></span>
»
<span class="breadcrumb-node">Path Hierarchy Tokenizer Examples</span>
</div>
<div class="navheader">
<span class="prev">
<a href="analysis-pathhierarchy-tokenizer.html">« Path Hierarchy Tokenizer</a>
</span>
<span class="next">
<a href="analysis-pattern-tokenizer.html">Pattern Tokenizer »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="analysis-pathhierarchy-tokenizer-examples"></a>Path Hierarchy Tokenizer Examples<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/analysis/tokenizers/pathhierarchy-tokenizer-examples.asciidoc">edit</a>
</h2>
</div></div></div>
<p>A common use-case for the <code class="literal">path_hierarchy</code> tokenizer is filtering results by
file paths. If indexing a file path along with the data, the use of the
<code class="literal">path_hierarchy</code> tokenizer to analyze the path allows filtering the results
by different parts of the file path string.</p>
<p>This example configures an index to have two custom analyzers and applies
those analyzers to multifields of the <code class="literal">file_path</code> text field that will
store filenames. One of the two analyzers uses reverse tokenization.
Some sample documents are then indexed to represent some file paths
for photos inside photo folders of two different users.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT file-path-test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "custom_path_tree": {
          "tokenizer": "custom_hierarchy"
        },
        "custom_path_tree_reversed": {
          "tokenizer": "custom_hierarchy_reversed"
        }
      },
      "tokenizer": {
        "custom_hierarchy": {
          "type": "path_hierarchy",
          "delimiter": "/"
        },
        "custom_hierarchy_reversed": {
          "type": "path_hierarchy",
          "delimiter": "/",
          "reverse": "true"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "file_path": {
        "type": "text",
        "fields": {
          "tree": {
            "type": "text",
            "analyzer": "custom_path_tree"
          },
          "tree_reversed": {
            "type": "text",
            "analyzer": "custom_path_tree_reversed"
          }
        }
      }
    }
  }
}

POST file-path-test/_doc/1
{
  "file_path": "/User/alice/photos/2017/05/16/my_photo1.jpg"
}

POST file-path-test/_doc/2
{
  "file_path": "/User/alice/photos/2017/05/16/my_photo2.jpg"
}

POST file-path-test/_doc/3
{
  "file_path": "/User/alice/photos/2017/05/16/my_photo3.jpg"
}

POST file-path-test/_doc/4
{
  "file_path": "/User/alice/photos/2017/05/15/my_photo1.jpg"
}

POST file-path-test/_doc/5
{
  "file_path": "/User/bob/photos/2017/05/16/my_photo1.jpg"
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/875.console"></div>
<p>A search for a particular file path string against the text field matches all
the example documents, with Bob’s documents ranking highest due to <code class="literal">bob</code> also
being one of the terms created by the standard analyzer boosting relevance for
Bob’s documents.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET file-path-test/_search
{
  "query": {
    "match": {
      "file_path": "/User/bob/photos/2017/05"
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/876.console"></div>
<p>It’s simple to match or filter documents with file paths that exist within a
particular directory using the <code class="literal">file_path.tree</code> field.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET file-path-test/_search
{
  "query": {
    "term": {
      "file_path.tree": "/User/alice/photos/2017/05/16"
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/877.console"></div>
<p>With the reverse parameter for this tokenizer, it’s also possible to match
from the other end of the file path, such as individual file names or a deep
level subdirectory. The following example shows a search for all files named
<code class="literal">my_photo1.jpg</code> within any directory via the <code class="literal">file_path.tree_reversed</code> field
configured to use the reverse parameter in the mapping.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET file-path-test/_search
{
  "query": {
    "term": {
      "file_path.tree_reversed": {
        "value": "my_photo1.jpg"
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/878.console"></div>
<p>Viewing the tokens generated with both forward and reverse is instructive
in showing the tokens created for the same file path value.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST file-path-test/_analyze
{
  "analyzer": "custom_path_tree",
  "text": "/User/alice/photos/2017/05/16/my_photo1.jpg"
}

POST file-path-test/_analyze
{
  "analyzer": "custom_path_tree_reversed",
  "text": "/User/alice/photos/2017/05/16/my_photo1.jpg"
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/879.console"></div>
<p>It’s also useful to be able to filter with file paths when combined with other
types of searches, such as this example looking for any files paths with <code class="literal">16</code>
that also must be in Alice’s photo directory.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET file-path-test/_search
{
  "query": {
    "bool" : {
      "must" : {
        "match" : { "file_path" : "16" }
      },
      "filter": {
        "term" : { "file_path.tree" : "/User/alice" }
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/880.console"></div>
</div>
<div class="navfooter">
<span class="prev">
<a href="analysis-pathhierarchy-tokenizer.html">« Path Hierarchy Tokenizer</a>
</span>
<span class="next">
<a href="analysis-pattern-tokenizer.html">Pattern Tokenizer »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>